#!/usr/bin/python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/03 11:28
# @Author  : Eric.Mao
# @FileName: ws.py
# @Software: PyCharm
# @Blog    : http://www.dev-share.top/

import asyncio
import websockets
import rndwalk
import json


# 接收客户端消息， 使用 (无限循环 + 线程阻塞) 技术来实践会话监听
async def recv_msg(websocket, path):
    # 无限循环
    while True:
        # 线程阻塞， 等待接收客户端信息
        recv_text = await websocket.recv()
        print(f'{recv_text}')

        # 向客户端发送信息
        response_text = f"your submit context: {recv_text}"
        await websocket.send(response_text)


async def send_msg(websocket, path):
    man_points = rndwalk.get_points()
    data = json.dumps(man_points)
    await websocket.send(data)

async def send_outlier(websocket, path):
    outlier_info = rndwalk.get_outlier()
    
    data = json.dumps(outlier_info)
    await websocket.send(data)






if __name__ == '__main__':
    # server = websockets.serve(send_msg, '172.18.145.221', 8765)
    server = websockets.serve(send_outlier, 'localhost', 8765)    
    asyncio.get_event_loop().run_until_complete(server)
    asyncio.get_event_loop().run_forever()

